//
//  NumberKeyboardAndToolTextField.m
//  Yanwei
//
//  Created by 刘文 on 17/4/24.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "NumberKeyboardAndToolTextField.h"

@interface NumberKeyboardAndToolTextField () {
    int _limitLength;
}

@end

@implementation NumberKeyboardAndToolTextField

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if(action == @selector(paste:))//禁止粘贴
        return NO;
    
    if(action == @selector(select:))// 禁止选择
        return NO;
    
    if(action == @selector(selectAll:))// 禁止全选
        return NO;
    
    return [super canPerformAction:action withSender:sender];
}

#pragma mark - methods
- (void)initTopView {
    self.inputAccessoryView = [self topView];
}

- (UIView *)topView {
    UIView *topview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    topview.backgroundColor = HEXCOLOR(0xd2d5da);
    
    CGFloat buttonWidth = 50;
    CGFloat buttonHeight = 30;
    CGFloat leftDistance = 16;
    
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitleColor:HEXCOLOR(0x505050) forState:UIControlStateNormal];
    cancelBtn.tag = 10;
    [topview addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topview).with.offset(leftDistance);
        make.centerY.equalTo(topview);
        make.width.mas_equalTo(buttonWidth);
        make.height.mas_equalTo(buttonHeight);
    }];
    
    UIButton * finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    [finishBtn setTitleColor:Color175030 forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
    finishBtn.tag = 11;
    [topview addSubview:finishBtn];
    [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topview).with.offset(-leftDistance);
        make.centerY.equalTo(cancelBtn);
        make.width.equalTo(cancelBtn);
        make.height.equalTo(cancelBtn);
    }];
    
    
//    NSArray *btnTitles = @[@"取消", @"完成"];
//    for (int i=0; i<2; i++) {
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.frame = CGRectMake(leftDistance + (ScreenWidth - leftDistance * 2 - buttonWidth * 2 + buttonWidth) * i, (CGRectGetHeight(topview.frame) - buttonHeight) / 2.0, buttonWidth, buttonHeight);
//        btn.backgroundColor = [UIColor blueColor];
//        [btn setTitle:btnTitles[i] forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
//        btn.tag = 10+i;
//        [topview addSubview:btn];
//    }
    
    // 标记
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(leftDistance+buttonWidth + 10, 0, ScreenWidth - leftDistance * 2 - buttonWidth * 2 - 20, CGRectGetHeight(topview.frame))];
    label.text = self.prompts;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];
    [topview addSubview:label];
    
    return topview;
}

- (void)limitTextLength:(int)length {
    _limitLength = length;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitEvent) name:UITextFieldTextDidChangeNotification object:self];
}

- (void)limitEvent {
    if ([self.text length] > _limitLength) {
        self.text = [self.text substringToIndex:_limitLength];
    }
}

#pragma mark - buttonEvent
- (void)btnEvent:(UIButton *)button {
    [self resignFirstResponder];
    
    if (button.tag == 11) {
        if (_block) {
            _block();
        }
    }
}

#pragma mark - removeNotification
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
